﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.ComponentModel;

namespace WindowsFormsApplication2
{
    class SQLConnection
    {
        /// <summary>
        /// Zwraca sqlCom
        /// </summary>
        /// <returns></returns>
        System.Data.SqlClient.SqlCommand GetSqlCommand()
        {
           // var model = new Models.TexturesEntities(this.serverConnectionString);

            System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection(this.serverConnectionString);//zly format
            SqlCon.Open();//login failed
            System.Data.SqlClient.SqlCommand SqlCom = new System.Data.SqlClient.SqlCommand();
            SqlCom.Connection = SqlCon;
            SqlCom.CommandType = CommandType.StoredProcedure;
            SqlCom.CommandText = "sp_databases";
            return SqlCom;
        }

        public void model()
        {
            var model = new Models.TexturesEntities(this.serverConnectionString + "; Initial Catalog=" + this.databaseName);

            var listOfGuids = model.User.Where(u => u.UserName == "test666" || u.UserName == "hello" || u.UserName == "Mailk" || u.UserName == "asdasdasd").Select(guid => new { guid.UserIdentity});
            var listOfFeaturGroups = model.FeatureGroupUser.GroupBy(c => c.FeatureGroupUserFeatureGroupId);
            var newRow = new Models.FeatureGroupUser();
            foreach(var listOfGuidsItem in listOfGuids)
            {
                foreach (var item in listOfFeaturGroups)
                    {
                    newRow.FeatureGroupUserUserIdentity = listOfGuidsItem.UserIdentity;
                    newRow.FeatureGroupUserFeatureGroupId = item.Key;
                    }       
                
            }
           // var asd = model.DeveloperAuthentication.Where(d => d.DeveloperAuthenticationUserIdentity == 1).ToList();
        }

        /// <summary>
        /// Zwraca listę baz danych która ma znaleźć się w comboboxie.
        /// </summary>
        /// <returns>Lista baz danych.</returns>
        public List<string> GetDatabasesComboBox()
        {
            List<string> databsesList = new List<string>();
            try
            {
                System.Data.SqlClient.SqlCommand SqlCom = this.GetSqlCommand();
                
                System.Data.SqlClient.SqlDataReader SqlDR;
                SqlDR = SqlCom.ExecuteReader();

                while (SqlDR.Read())
                {
                    databsesList.Add(SqlDR.GetString(0));
                }
            }
            catch (ArgumentException ex)
            {
               // throw new ExceptionH("format");
               // throw new ArgumentException("Zły format");
            }
            return databsesList;
        }
        
        /// <summary>
        /// Zwraca listę connection stringów by połączyc się z zerwerem, list pobrana z pliku.
        /// </summary>
        /// <param name="file">Plik.</param>
        /// <returns>Lista connection stringów serwera.</returns>
        public List<string> GetServerComboBoxFromFile(string file)
        {
            List<string> serverConnectionStringList = new List<string>();
            try
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        serverConnectionStringList.Add(line);
                    }
                }
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException("Brak pliku");
            }

            return serverConnectionStringList;
        }

        
        public string serverConnectionString { get; set; }
        public string databaseName { get; set; }
    }
}
